﻿using System;
using HsTec.Mobile.Green.BLL;
using System.Data;
using System.Web.UI.WebControls;
using System.Collections;
using HsTec.Mobile.Green.Util;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Text;

public partial class Indicators_Indicators_Query : System.Web.UI.Page
{
    private IndexFormulaBLL bll = new IndexFormulaBLL();
    protected string sAreaId = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_Pers_ID"] == null)
        {
            Response.Redirect("../Logon.aspx");
        }
        sAreaId = (string)Session["Baimasoft_Report"];
        lstIndi.Attributes.Add("onclick", "FindSelectedItems(this," + txtIndi.ClientID + ");");
        lstArea.Attributes.Add("onclick", "FindSelectedItems(this," + txtArea.ClientID + ");");
        chkIndi.Attributes.Add("onclick", "chkAll(this)");
        chkArea.Attributes.Add("onclick", "chkAll(this)");
        if (!this.IsPostBack)
        {
            try
            {
                Indicator.Text = Request.QueryString["sIndicator"].ToString();
            }
            catch
            {

            }
            GridView1.Attributes.Add("BorderColor", "LightSkyBlue");
            if (ViewState["sortDirection"] == null)
            {
                ViewState["sortDirection"] = " ASC";
            }
            if (ViewState["sortExpression"] == null)
            {
                ViewState["sortExpression"] = String.Empty;
            }
            this.sltAreaBind();
            this.sltIndiBind();
        }
    }
    private void sltIndiBind()
    {
        lstIndi.DataSource = bll.GetList("FormulaParent=3");
        lstIndi.DataTextField = "IndexName";
        lstIndi.DataValueField = "IF_ID";
        lstIndi.DataBind();
    }
    private void sltAreaBind()
    {
        lstArea.DataSource = bll.GetAreaList(sAreaId);
        lstArea.DataTextField = "Name";
        lstArea.DataValueField = "ID";
        lstArea.DataBind();
    }

    private DataView getInfo()
    {
        string sIndi = String.Empty;
        for (int i = 0; i < lstIndi.Items.Count; i++)
        {
            if (lstIndi.Items[i].Selected == true)
            {
                sIndi += sIndi == String.Empty ? lstIndi.Items[i].Value : "," + lstIndi.Items[i].Value;
            }
        }
        string sType = String.Empty;
        if (rddf.Checked)
            sType = "1";
        else
            sType = "2";
        string sArea = String.Empty;
        for (int i = 0; i < lstArea.Items.Count; i++)
        {
            if (lstArea.Items[i].Selected == true)
            {
                sArea += sArea == String.Empty ? lstArea.Items[i].Value : "," + lstArea.Items[i].Value;
            }
        }
        if (String.IsNullOrEmpty(sArea))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择地市信息!');</script>");
            return null;
        }
        DataView mydv = bll.GetIndicatorListNewArea(sltStartYear.Value.Trim() + sltStartMonth.Value.Trim(), sltEndYear.Value.Trim() + sltEndMonth.Value.Trim(), sArea, sIndi, sType);
        if (!String.IsNullOrEmpty(ViewState["sortExpression"].ToString()))
        {

            //try
            //{
            //    mydv.Sort = ViewState["sortExpression"].ToString() + ViewState["sortDirection"].ToString();
            //}
            //catch
            //{ 
            //}

            mydv = Util.SortDataView(mydv, ViewState["sortExpression"].ToString(), ViewState["sortDirection"].ToString());
        }
        return mydv;
    }
    private void BindGridViewData()
    {

        GridView1.DataSource = getInfo();
        GridView1.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (ViewState["sortExpression"] != null)
            ViewState["sortExpression"] = String.Empty;
        BindGridViewData();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        if (ViewState["sortExpression"] != null)
            ViewState["sortExpression"] = String.Empty;
        BindGridViewData();
    }
    protected void linkBtnGo_Click(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
    }
    protected void GridView1_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
    {
        if (ViewState["sortDirection"].ToString() == " ASC")
        {
            ViewState["sortDirection"] = " DESC";
        }
        else
        {
            ViewState["sortDirection"] = " ASC";
        }
        ViewState["sortExpression"] = e.SortExpression;
        this.BindGridViewData();
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        try
        {
            HttpContext curContext = System.Web.HttpContext.Current;
            StringWriter strWriter = new StringWriter();
            System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter);
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
            curContext.Response.Charset = "GB2312";
            DataView mydv = getInfo();
            GridView GV = new GridView();
            GV.DataSource = mydv;
            GV.DataBind();
            GV.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }
        catch
        {

        }
    }
}
